package vn.kiemviec.app.task;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Element;

import vn.kiemviec.app.utils.LogTools;
import vn.kiemviec.app.utils.XmlUtils;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.KeyEvent;

public abstract class CBTask<E> extends AsyncTask<Object, Object, Object>{

	public interface CBTaskListener<E>{
		public void onSuccessfull(ArrayList<E> listEntry);
		public void onError(int error);
	}
	
	
	private CBTaskListener<E> mHandler;
	public void setOnTaskListener(CBTaskListener<E> ev){
		mHandler = ev;
	}
	
	public abstract String onGetApiUrl();
	public abstract E getEntry(JSONObject jb);
	
	private boolean IsSuccessful;
	private ArrayList<E> mList = new ArrayList<E>();
	
	protected ProgressDialog dialog;
	private String dialogMessage = "loading....";
	private boolean enabledProgressDialog = true;
	private Context mContext;
	
	public CBTask(Context context){
		mContext = context;
	}
	
	public void setEnableProgress(boolean value){
		enabledProgressDialog = true;
	}
	
	public void setProgressMessage(String msg){
		
	}
	
	public void setProgressMessage(int msgRes){
		
	}
	
	@Override
	protected void onPreExecute() {
		
		if(!enabledProgressDialog) return;
		
		String message = dialogMessage;
		
		dialog = ProgressDialog.show(mContext, null, message);
		dialog.setCancelable(false);
		dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
			
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_SEARCH) {
					return true;
				}
				return false;
			}
		});
//		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
//
//			@Override
//			public void onCancel(DialogInterface _dialog) {
//				forceCloseDialog();
//				cancel(true);
//
//				if (null != cancelCallback) {
//					cancelCallback.onCancel(dialog);
//				}
//			}
//		});
	}
	
	@Override
	protected Object doInBackground(Object... arg0) {
		// TODO Auto-generated method stub
		try {
			
			Element root = XmlUtils.getXmlRootByUrl(onGetApiUrl());
			String status = root.getElementsByTagName("status").item(0).getTextContent();
			if(status.endsWith("success")){
				String data = root.getElementsByTagName("response").item(0).getTextContent();
//				LogTools.info("data:"+data);
				try {
					JSONArray jrr = new JSONArray(data);
					for(int i = 0;i<jrr.length();i++){
						mList.add(getEntry(jrr.getJSONObject(i)));
					}
				} catch (Exception e) {
				}
				IsSuccessful = true;
			}else{
				IsSuccessful = false;
			}

		} catch (Exception e) {
			// TODO: handle exception
			LogTools.error(e.toString());
			IsSuccessful = false;
			if(mHandler != null) mHandler.onError(0);
		}
		
		return null;
	}
	
	@Override
	protected void onPostExecute(Object result) {
		// TODO Auto-generated method stub
		if (null != this.dialog && this.dialog.isShowing()) {
			this.dialog.dismiss();
		}
		
		if(IsSuccessful){
			if(mHandler != null) mHandler.onSuccessfull(mList);
		}
		super.onPostExecute(result);
	}
}
